import 'dart:convert';
import 'dart:ui' as ui show window;

import 'package:flutter/material.dart';
import 'package:gw_github_flutter/common/gwNet.dart';
import 'package:gw_github_flutter/common/gwNetCache.dart';
import 'package:gw_github_flutter/models/index.dart';
import 'package:gw_github_flutter/tools/gwAutoSizeTool.dart';
import 'package:shared_preferences/shared_preferences.dart';

const _themes = <MaterialColor>[
  Colors.blue,
  Colors.cyan,
  Colors.teal,
  Colors.green,
  Colors.red,
];

class Global {
  static const gwRequestStateNoCache = "noCache";
  static const gwRequestStateRefrash = "refrash";
  static late SharedPreferences prefs;
  static Profile profile = Profile();
// 网络缓存对象
  static GwNetCache netCache = GwNetCache();
  // 可选的主题列表
  static List<MaterialColor> get themes => _themes;

  // 是否为release版
  static bool get isRelease => bool.fromEnvironment("dart.vm.product");

  static Future init() async {
    WidgetsFlutterBinding.ensureInitialized();
    // GwAutoWidgetsFlutterBinding.ensureInitialized();
    prefs = await SharedPreferences.getInstance();
    var _profile = prefs.getString(globalSaveKey.profile);
    if (_profile != null) {
      try {
        profile = Profile.fromJson(jsonDecode(_profile));
      } catch (e) {
        print(e);
      }
    } else {
      // 默认主题索引为0，代表蓝色
      profile = Profile()..theme = _themes.first.value;
    }

    // 如果没有缓存策略，设置默认缓存策略
    profile.cache = profile.cache ?? CacheConfig()
      ..enable = true
      ..maxAge = 3600
      ..maxCount = 100;

    //初始化网络请求相关配置
    GwNet.init();
  }

  // 持久化Profile信息
  static saveProfile() {
    prefs.setString(globalSaveKey.profile, jsonEncode(profile.toJson()));
  }

  static List<TbSectionModel> getTBSectionsList(
      [int sectionSize = 7, int itemSize = 5]) {
    var sections = List<TbSectionModel>.empty(growable: true);
    for (int i = 0; i < sectionSize; i++) {
      var section = TbSectionModel()
        ..header = "Header#$i"
        ..items = List.generate(itemSize, (index) => "ListTile #$index")
        ..expanded = true;
      sections.add(section);
    }
    return sections;
  }

  /// 屏幕宽
  ///
  static double get width {
    MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
    return mediaQuery.size.width;
  }

  /// RPX 用于屏幕适配（比例适配）
  ///
  static double get rpx {
    MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
    return mediaQuery.size.width / 750;
  }

  /// 屏幕高
  ///
  static double get height {
    MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
    return mediaQuery.size.height;
  }

  /// 标题栏高度（包括状态栏）
  ///
  static double get navigationBarHeight {
    MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
    return mediaQuery.padding.top + kToolbarHeight;
  }

  /// 状态栏高度
  ///
  static double get topSafeHeight {
    MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
    return mediaQuery.padding.top;
  }

  /// 底部状态栏高度
  ///
  static double get bottomSafeHeight {
    MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
    return mediaQuery.padding.bottom;
  }
}

class globalSaveKey {
  static const String firstPage = "firstPage";
  static const String profile = "profile";

}

// ignore: camel_case_extensions
extension globalSizeExtension on num{
  double get realPx => Global.rpx * this;
}
